const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    traineePhone: null,
    balance: 0,
    flexibility: 0,
    coordination: 0,
    cardiorespiratory: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    const traineePhone = wx.getStorageSync('traineePhone');
    this.setData({
      traineePhone
    });
    const that = this;
    const url = app.globalData.url;
    wx.request({
      url: url + '/train/bodyAdaptability/' + traineePhone,
      success: function(response) {
        if (response.data.success) {
          const bodyObj = response.data.content;
          that.setData({
            balance: bodyObj.balance,
            flexibility: bodyObj.flexibility,
            coordination: bodyObj.coordination,
            cardiorespiratory: bodyObj.cardiorespiratory
          });
        }
      }
    });
  },

  bindBalance: function(e) {
    this.setData({
      balance: e.detail.value
    });
  },
  bindSoft: function(e) {
    this.setData({
      flexibility: e.detail.value
    });
  },
  bindCoordination: function(e) {
    this.setData({
      coordination: e.detail.value
    });
  },
  bindHeart: function(e) {
    this.setData({
      cardiorespiratory: e.detail.value
    });
  },
  save: function() {
    const url = app.globalData.url;
    const balance = this.data.balance;
    const flexibility = this.data.flexibility;
    const coordination = this.data.coordination;
    const cardiorespiratory = this.data.cardiorespiratory;
    const phone = this.data.traineePhone;
    wx.request({
      url: url + '/train/saveBodyAdaptability',
      data: {
        phone,
        balance,
        flexibility,
        coordination,
        cardiorespiratory
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '保存成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '保存失败',
            icon: 'none'
          })
        }
      }
    })
  }
})